<template>
    <div class="content-main">
        <div class="breadcrumb">
            <div class="breadcrumb-icon">
                <img src="./../../assets/images/breadcrumb_icon.png" />
            </div>
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/admin/classMgt' }">班级管理</el-breadcrumb-item>
                <el-breadcrumb-item>修改班级</el-breadcrumb-item>
            </el-breadcrumb>
            <bread-crumb-back></bread-crumb-back>
        </div>
        <div class="content-box">
            <!-- <div class="teaching-mgt-tips" style="margin:">
                <span>提示: </span>
                选择案例部分“蓝色”代表选项已结项，“灰色”选项代表未结项。
            </div> -->
            <div class="add-class-form">
                <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
                    <el-form-item
                        style="width:60%;"
                        prop="className"
                        label="班级名称"
                        :rules="[
                            { required: true, message: '请输入班级名称', trigger: 'blur' }
                        ]"
                        >
                        <el-input v-model="dynamicValidateForm.className"></el-input>
                    </el-form-item>
                    <div
                        style="width:900px;"
                        v-for="(domain, index) in dynamicValidateForm.domains"
                    >
                        <el-form-item
                            style="width:300px; float:left; margin-right: 20px;"
                            :label="'选择教师'"
                            :prop="'domains.' + index + '.teacherValue'"
                            :rules="{
                                required: true, message: '请选择教师', trigger: 'blur'
                            }"
                            >
                            <el-select
                                v-model="domain.teacherValue"
                                :disabled="domain.disabled==true"
                                placeholder="请选择教师"
                                value-key="value"
                                style="float:left;">
                                <el-option
                                v-for="item in domain.teacherStatus"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item
                            style="float:left;"
                            :label="'选择案例'"
                            :prop="'domains.' + index + '.projectValue'"
                            disabled
                            :rules="{
                                required: true, message: '请选择案例', trigger: 'blur'
                            }"
                            >
                            <el-cascader-multi  style="width:310px;" v-model="domain.projectValue" :show-leaf-label="true" collapse-tags placeholder="请选择案例" :data="domain.projectStatus"> </el-cascader-multi>
                            <!-- <el-select
                                v-model="domain.projectValue"
                                placeholder="请选择案例"
                                multiple
                                collapse-tags
                                value-key="value"
                                style="float:left;"
                                @change="projectChange">
                                <el-option
                                v-for="(item, index2) in domain.projectStatus"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select> -->
                        </el-form-item>
                        <el-tag style="float:left; margin:5px 10px 0 0;" v-if="domain.endMark == 0 || domain.endMark == 1">{{ domain.endMark == 0 ? '未结项' : '已结项'}}</el-tag>
                        <el-button style="float:left; margin-left:10px;" @click.prevent="removeDomain(domain)">删除</el-button>
                    </div>
                    <el-form-item style="float:left;">
                        <el-button type="primary" @click="submitForm('dynamicValidateForm')">保存</el-button>
                        <el-button @click="addDomain">新增案例</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    import BreadCrumbBack from '@/components/breadCrumbBack.vue'
    export default {
        components: {
            BreadCrumbBack
        },
        data () {
            return {
                dynamicValidateForm: {
                    domains: [
                    ],
                    className: ''
                },
                relationList: [],
                //单独保存下拉数据
                teacherStatus: [],
                projectStatus: []
            }
        },
        created () {
            this.init() //初始化
        },
        methods: {
            initSelect(){
                let self = this
                self.$http.get('/dataplatform/major/tree').then((response) => {
                    let res = response.data
                    if(res.code == '200') {
                        for(let i = 0; i < res.data.length; i++){
                            let selectJson = {}
                            selectJson.label = res.data[i].name
                            selectJson.value = res.data[i].id
                            selectJson.children = []
                            if(res.data[i].itemList){
                                for(let j = 0; j < res.data[i].itemList.length; j++){
                                    let selectJson2 = {}
                                    selectJson2.label = res.data[i].itemList[j].name
                                    selectJson2.value = res.data[i].itemList[j].id
                                    selectJson.children.push(selectJson2)
                                }
                            }

                            self.dynamicValidateForm.domains[0].projectStatus.push(selectJson)
                            self.projectStatus.push(selectJson)
                        }
                    }else if(res.code == '401'){
                        self.$router.replace({
                            path:'/login'
                        })
                    }else{
                        self.$message.error(res.message)
                    }
                })
            },
            init() {
                let self = this
                self.$http.get('/dataplatform/system/class/getSaveClassParamDetail?id='+self.$route.query.id).then((response) => {
                    let res = response.data
                    if(res.code == '200') {
                        self.dynamicValidateForm.className = res.data.className
                        for(let i = 0; i < res.data.classRelationParamDetailList.length; i++){
                            let selectJson = {
                                teacherStatus: [],
                                teacherValue: '',
                                projectStatus: [],
                                projectValue: [],
                                endMarkArr: [],
                                disabled: true,
                                endMark: ''
                            }
                            for(let j = 0; j < res.data.classRelationParamDetailList[i].majorItemMatrixList.length; j++){
                                selectJson.projectValue.push(res.data.classRelationParamDetailList[i].majorItemMatrixList[j])
                                selectJson.endMarkArr.push(res.data.classRelationParamDetailList[i].relationVOList[j].endMark)
                            }

                            selectJson.teacherValue = res.data.classRelationParamDetailList[i].userId
                            selectJson.endMark = res.data.classRelationParamDetailList[i].endMark
                            for (let j = 0; j < res.data.teacherList.length; j++) {
                                let selectJson3 = {}
                                selectJson3.label = res.data.teacherList[j].userName
                                selectJson3.value = res.data.teacherList[j].id
                                selectJson.teacherStatus.push(selectJson3)
                                self.teacherStatus.push(selectJson3)
                            }
                            // for (let j = 0; j < res.data.teachItemList.length; j++) {
                            //     let selectJson3 = {}
                            //     selectJson3.label = res.data.teachItemList[j].name
                            //     selectJson3.value = res.data.teachItemList[j].id
                            //     for(let e = 0; e < selectJson.projectValue.length; e++){
                            //         if(res.data.teachItemList[j].id == selectJson.projectValue[e] && selectJson.endMarkArr[e] == 1){
                            //             selectJson3.endMark = true
                            //         }
                            //     }
                            //     selectJson.projectStatus.push(selectJson3)
                            // }
                            for(let j = 0; j < res.data.majorItemTree.length; j++){
                                let selectJson3 = {}
                                selectJson3.label = res.data.majorItemTree[j].name
                                selectJson3.value = res.data.majorItemTree[j].id
                                selectJson3.children = []
                                if(res.data.majorItemTree[j].itemList){
                                    for(let e = 0; e < res.data.majorItemTree[j].itemList.length; e++){
                                        let selectJson4 = {}
                                        selectJson4.label = res.data.majorItemTree[j].itemList[e].name
                                        selectJson4.value = res.data.majorItemTree[j].itemList[e].id
                                        selectJson3.children.push(selectJson4)
                                    }
                                }

                                selectJson.projectStatus.push(selectJson3)
                                self.projectStatus.push(selectJson3)
                            }
                            self.dynamicValidateForm.domains.push(selectJson)
                        }
                    }else if(res.code == '401'){
                        self.$router.replace({
                            path:'/login'
                        })
                    }else{
                        self.$message.error(res.message)
                    }
                })
            },
            //提交表单
            submitForm(formName) {
                let self = this
                self.$refs[formName].validate((valid) => {
                    if (valid) {
                        const loading = this.$loading({
                            lock: true,
                            text: '保存中',
                            spinner: 'el-icon-loading',
                            background: 'rgba(0, 0, 0, 0.7)'
                        })
                        let relationList = []
                        for(let i = 0; i < self.dynamicValidateForm.domains.length; i++){
                            let selectJson = {}
                            selectJson.projectIdList = []
                            for(let k=0; k<self.dynamicValidateForm.domains[i].projectValue.length; k++){
                                selectJson.projectIdList.push(self.dynamicValidateForm.domains[i].projectValue[k][1])
                                //console.log(selectJson.projectIdList,123)
                            }
                            //selectJson.projectIdList = self.dynamicValidateForm.domains[i].projectValue
                            selectJson.userId = self.dynamicValidateForm.domains[i].teacherValue
                            relationList.push(selectJson)
                            
                        }
                        
                        self.$http.post('/dataplatform/system/class/saveClass',{
                            id: self.$route.query.id,
                            className: self.dynamicValidateForm.className,
                            relationList: relationList
                        }).then((response) => {
                            let res = response.data
                            if(res.code == '200') {
                                loading.close()
                                self.$message({
                                    message: '保存成功',
                                    type: 'success'
                                })
                                self.$router.push({
                                    path:'/admin/classMgt'
                                })
                            }else if(res.code == '401'){
                                loading.close()
                                self.$router.replace({
                                    path:'/login'
                                })
                            }else{
                                loading.close()
                                self.$message.error(res.message)
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            //删除案例
            removeDomain(item) {
                var index = this.dynamicValidateForm.domains.indexOf(item)
                if (index !== -1) {
                    this.dynamicValidateForm.domains.splice(index, 1)
                }
            },
            //添加案例
            addDomain() {
                let self = this
                if(self.dynamicValidateForm.domains.length == 0){
                    let selectJson = {
                        teacherStatus: [],
                        teacherValue: '',
                        projectStatus: [],
                        projectValue: ''
                    }
                    self.$http.post('/dataplatform/system/class/getAllTeachers').then((response) => {
                        let res = response.data
                        if(res.code == '200') {
                            for(let i = 0; i < res.data.length; i++){
                                let selectJson2 = {}
                                selectJson2.label = res.data[i].userName
                                selectJson2.value = res.data[i].id
                                selectJson.teacherStatus.push(selectJson2)
                            }
                        }else if(res.code == '401'){
                            self.$router.replace({
                                path:'/login'
                            })
                        }else{
                            self.$message.error(res.message)
                        }
                    })
                    // self.$http.post('/dataplatform/system/class/getReleaseProjectList?type=1').then((response) => {
                    //     let res = response.data
                    //     if(res.code == '200') {
                    //         for(let i = 0; i < res.data.length; i++){
                    //             let selectJson2 = {}
                    //             selectJson2.label = res.data[i].name
                    //             selectJson2.value = res.data[i].id
                    //             selectJson.projectStatus.push(selectJson2)
                    //         }
                    //     }else if(res.code == '401'){
                    //         self.$router.replace({
                    //             path:'/login'
                    //         })
                    //     }else{
                    //         self.$message.error(res.message)
                    //     }
                    // })
                    self.$http.get('/dataplatform/major/tree').then((response) => {
                        let res = response.data
                        if(res.code == '200') {
                            for(let i = 0; i < res.data.length; i++){
                                let selectJson2 = {}
                                selectJson2.label = res.data[i].name
                                selectJson2.value = res.data[i].id
                                selectJson2.children = []
                                if(res.data[i].itemList){
                                    for(let j = 0; j < res.data[i].itemList.length; j++){
                                        let selectJson3 = {}
                                        selectJson3.label = res.data[i].itemList[j].name
                                        selectJson3.value = res.data[i].itemList[j].id
                                        selectJson2.children.push(selectJson3)
                                    }
                                }

                                // self.dynamicValidateForm.domains[0].projectStatus.push(selectJson)
                                // self.projectStatus.push(selectJson)
                                selectJson.projectStatus.push(selectJson2)
                            }
                        }else if(res.code == '401'){
                            self.$router.replace({
                                path:'/login'
                            })
                        }else{
                            self.$message.error(res.message)
                        }
                    })
                    self.dynamicValidateForm.domains.push(selectJson);
                }else{
                    var domainArr = {
                        teacherStatus: self.dynamicValidateForm.domains[0].teacherStatus,
                        teacherValue: '',
                        projectStatus: self.dynamicValidateForm.domains[0].projectStatus,
                        projectValue: []
                    }
                    self.dynamicValidateForm.domains.push(domainArr);
                }

            },
            projectChange(){
                console.log(this.dynamicValidateForm.domains)
            }
        }
    }
</script>
<style>
.el-select-dropdown.el-popper.is-multiple.multi-cascader-style .li-style .li-label-style{
    line-height :34px;
}
</style>
